/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package net.sf.l2j.gameserver.datatables;

import java.io.File;
import java.util.List;
import java.util.logging.Logger;

import javolution.util.FastList;

import net.sf.l2j.Config;
import net.sf.l2j.gameserver.model.FishData;
import net.sf.l2j.gameserver.xmlfactory.XMLDocumentFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
 * @author -Nemesiss-
 */
public class FishTable
{
	private static Logger _log = Logger.getLogger(FishTable.class.getName());
	
	private static List<FishData> _fishsNormal;
	private static List<FishData> _fishsEasy;
	private static List<FishData> _fishsHard;
	
	public static FishTable getInstance()
	{
		return SingletonHolder._instance;
	}
	
	private FishTable()
	{
		try
		{
			_fishsEasy = new FastList<FishData>();
			_fishsNormal = new FastList<FishData>();
			_fishsHard = new FastList<FishData>();
			
			File f = new File(Config.DATAPACK_ROOT + "/data/xml/fishes.xml");
			Document doc = XMLDocumentFactory.getInstance().loadDocument(f);
			
			Node n = doc.getFirstChild();
			for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
			{
				if (d.getNodeName().equalsIgnoreCase("fish"))
				{
					int id = Integer.parseInt(d.getAttributes().getNamedItem("id").getNodeValue());
					int lvl = Integer.parseInt(d.getAttributes().getNamedItem("level").getNodeValue());
					String name = String.valueOf(d.getAttributes().getNamedItem("name").getNodeValue());
					int hp = Integer.parseInt(d.getAttributes().getNamedItem("hp").getNodeValue());
					int hpreg = Integer.parseInt(d.getAttributes().getNamedItem("hpregen").getNodeValue());
					int type = Integer.parseInt(d.getAttributes().getNamedItem("fish_type").getNodeValue());
					int group = Integer.parseInt(d.getAttributes().getNamedItem("fish_group").getNodeValue());
					int fish_guts = Integer.parseInt(d.getAttributes().getNamedItem("fish_guts").getNodeValue());
					int guts_check_time = Integer.parseInt(d.getAttributes().getNamedItem("guts_check_time").getNodeValue());
					int wait_time = Integer.parseInt(d.getAttributes().getNamedItem("wait_time").getNodeValue());
					int combat_time = Integer.parseInt(d.getAttributes().getNamedItem("combat_time").getNodeValue());
					
					FishData fish = new FishData(id, lvl, name, hp, hpreg, type, group, fish_guts, guts_check_time, wait_time, combat_time);
					switch (fish.getGroup())
					{
						case 0:
							_fishsEasy.add(fish);
							break;
						case 1:
							_fishsNormal.add(fish);
							break;
						case 2:
							_fishsHard.add(fish);
					}
				}
			}
		}
		catch (Exception e)
		{
			_log.warning("FishTable: Error while creating table" + e);
		}
		
		int count = _fishsEasy.size() + _fishsNormal.size() + _fishsHard.size();
		_log.info("FishTable: Loaded " + count + " fishes.");
	}
	
	/**
	 * @param lvl The level of the fish.
	 * @param type The type of the fish.
	 * @param group The group of the fish.
	 * @return List of Fish that can be fished
	 */
	public List<FishData> getfish(int lvl, int type, int group)
	{
		List<FishData> result = new FastList<FishData>();
		List<FishData> _Fishs = null;
		switch (group)
		{
			case 0:
				_Fishs = _fishsEasy;
				break;
			case 1:
				_Fishs = _fishsNormal;
				break;
			case 2:
				_Fishs = _fishsHard;
		}
		
		// the fish list is empty
		if (_Fishs == null)
		{
			_log.warning("Fish are not defined !");
			return null;
		}
		
		for (FishData f : _Fishs)
		{
			if (f.getLevel() != lvl)
				continue;
			if (f.getType() != type)
				continue;
			
			result.add(f);
		}
		
		if (result.size() == 0)
			_log.warning("Couldn't find any fish with lvl: " + lvl + " and type: " + type);
		
		return result;
	}
	
	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder
	{
		protected static final FishTable _instance = new FishTable();
	}
}